

#import "BIDSettingsTableViewController.h"

@interface BIDSettingsTableViewController ()

@end

@implementation BIDSettingsTableViewController{
    NSInteger currentIndex; //current index of self.repositories
}

- (NSString *)dataFilePath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:@"data.archive"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    currentIndex = -1;
    self.sections = @[@"Repository", @"Storage", @"Display"];
    self.storage = @[@"Size", @"Update type"];
    self.display = @[@"Color", @"Number of tabs"];
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
        NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        self.repository = [unarchiver decodeObjectForKey:@"repository"];
        [unarchiver finishDecoding];
    }
    /*
    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
     */
}
/*
- (void)applicationWillResignActive:(NSNotification *)notification{
    NSString *filePath = [self dataFilePath];
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:self.repository forKey:@"repository"];
    [archiver finishEncoding];
    [data writeToFile:filePath atomically:YES];
}
 */
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [self.sections count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if ([self.sections[section] isEqualToString:@"Repository"]){
        if (self.repository == nil){
            return 1;
        }else{
            return 2; //list of repositories plus "Add repository"
        }
    }else if ([self.sections[section] isEqualToString:@"Display"]){
        return [self.display count];
    }else{
        return [self.storage count];
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.sections[section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //static NSString *CellIdentifier = @"disclosureCell";
    UITableViewCell *cell;
    // Configure the cell...
    if ([self.sections[indexPath.section] isEqualToString:@"Repository"]){
        if (indexPath.row == 0){
            cell = [tableView dequeueReusableCellWithIdentifier:@"disclosureCell" forIndexPath:indexPath];
            if (self.repository == nil){
                cell.textLabel.text = @"Add repository";
            }else{
                cell.textLabel.text = @"Edit repository";
            }
        }else{
            cell = [tableView dequeueReusableCellWithIdentifier:@"plainCell" forIndexPath:indexPath];
            //BIDRepository *repository = self.repositories[indexPath.row];
            cell.textLabel.text = self.repository.name;
            //cell.selected = NO;
        }
    }else if ([self.sections[indexPath.section] isEqualToString:@"Display"]){
        cell = [tableView dequeueReusableCellWithIdentifier:@"disclosureCell" forIndexPath:indexPath];
        cell.textLabel.text = self.display[indexPath.row];
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"disclosureCell" forIndexPath:indexPath];
        cell.textLabel.text = self.storage[indexPath.row];
    }
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    currentIndex = indexPath.row - 1; //current index of self.repositories
}

// Set proper name to SettingsDetailTalbeView
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    UITableViewController *destination = segue.destinationViewController;
    if ([destination respondsToSelector:@selector(setDelegate:)]){
        [destination setValue:self forKey:@"delegate"];
    }
    if ([destination respondsToSelector:@selector(setRepository:)] /*&& currentIndex >= 0*/)  {
        [destination setValue:self.repository forKey:@"repository"];
    }
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    destination.title = self.sections[indexPath.section];
}
- (void)settingsDetailTableViewController:(BIDSettingsDetailTableViewController *)controller addRepository:(BIDRepository *)repository{
    //now we allow ONLY 1 repository tobe added
    if (self.repository == nil){
        self.repository = [[BIDRepository alloc] init];
    }
    self.repository = repository;
    [self.tableView reloadData];
}
@end
